<?php
/**
 * 分页类
 * 属性为public类型以便可以灵活使用,
 * 比如把实现$this->setStyle()的功能放在调用该类的页面中(不用extends)完成
 * 
 * 待修改
 *
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2011 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
class UE_Paging {

	/**
	 * 提供输出的信息
	 *
	 * @var String
	 */
	public $pagingStr = '';

	/**
	 * 总记录数
	 *
	 * @var int
	 */
	public $totals = 100;
	
	/**
	 * 每页显示记录数
	 *
	 * @var int
	 */
	public $pageNo = 10;

	/**
	 * 上一页的页码
	 *
	 * @var int
	 */
	public $lastPage;

	/**
	 * 上一页的页码
	 *
	 * @var int
	 */
	public $prePage;

	/**
	 * 下一页的页码
	 *
	 * @var int
	 */
	public $nextPage;

	/**
	 * 分页的url查询变量
	 *
	 * @var string
	 */
	public $pageSplitVar = 'page';

	/**
	 * 分页页的uri
	 *
	 * @var string
	 */
	public $uri;

	/**
	 * 查询的起始项
	 *
	 * @var int
	 */
	public $firstCount;

	public $pageNoVar = 'pageNo';
	
	/**
	 * url参数分隔符
	 *
	 * @var string(char)
	 */
	public $argSeparator = '/';
	
	/**
	 * url参数等号符号
	 *
	 * @var string(char)
	 */
	public $argEQSign = '=';
	
	public function __construct() {

	}

	/**
	 * 分页赋值
	 *
	 * @param int $total
	 * @param int $pageNo
	 * @param string $pageSplitVar
	 * @param string $uri
	 */
	public function setVar($total, $pageNo=10, $uri='', $pageSplitVar = 'page', $pageNoVar = 'pageNo', $setStyle = 1) {
        
		$this->totals         = $total;
		$this->pageNo         = empty($_GET[$pageNoVar]) ? $pageNo : $_GET[$pageNoVar];
		$this->pageSplitVar   = $pageSplitVar;
		$this->pageNoVar      = $pageNoVar;

		/* 分页页码(要打开第n页) */
		if(empty($_GET[$this->pageSplitVar]) || (int)$_GET[$this->pageSplitVar] <= 0) {
			$this->page = 1;
		} else {
			$this->page = (float) $_GET[$this->pageSplitVar];
		}

		$_REQUEST['app'] = $_GET['app'];
		$_REQUEST['ctl'] = $_GET['ctl'];
		$_REQUEST['act'] = $_GET['act'];
		$_REQUEST['id'] = $_GET['id'];

		unset($_GET[$this->pageNo]);
		unset($_GET[$this->pageSplitVar]);
        $this->uri = '?';
		foreach($_GET as $key=> $val) {
			$this->uri .= "&$key=".urlencode($val);
		}
		/*
		foreach($_POST as $key=> $val) {
			$this->uri .= "&$key=".urlencode($val);
		}
		*/
		
		$this->uri = str_replace("?&", '?', $this->uri);

		/* 页码计算 */
		$this->lastPage    = ceil($this->totals / $this->pageNo);         // 最后页，也是总页数
		$this->page        = min($this->lastPage, $this->page);              // page值超过最大值时取最大值做page值
		$this->prePage     = $this->page - 1;                                // 上一页
		$this->nextPage    = ($this->page == $this->lastPage) ? 0 : $this->page + 1; // 下一页
		$this->firstCount  = $this->page ? ($this->page - 1) * $this->pageNo : 0;              // 查询的起始项

		if ($setStyle) {
			$this->setStyle();
		}
	}

	/**
	 * 分页导航条输出模板/样式
	 *
	 * @return bool
	 */
	public function setStyle(){
		/* 开始分页导航条代码 */

		/* 如果只有一页则跳出方法 */
		//if($this->lastPage <= 1) return false;
		
		/* 首页 */
		$this->pagingStr .= "
		<div class='page_nav'>
		 <a id=paging_total href class='button' title='总主题数'>共{$this->totals}条</a><a id=paging_page_no title='当前页/总页数' class='button'>{$this->page}/{$this->lastPage}</a>
		 <a id=paging_first href='{$this->uri}&{$this->pageNoVar}={$this->pageNo}'  class='button'>头页</a>";

		/* 前页 */
		if($this->prePage) {
			$this->pagingStr.="<a id=paging_last href='{$this->uri}&{$this->pageSplitVar}={$this->prePage}&{$this->pageNoVar}={$this->pageNo}' class='button'>上一页</a>";
		} else {
			$this->pagingStr.="<a class='button' type='button'>上一页</a>";
		}

		/* 后页 */
		if($this->nextPage) {
			$this->pagingStr .= "<a id=paging_next href='{$this->uri}&{$this->pageSplitVar}={$this->nextPage}&{$this->pageNoVar}={$this->pageNo}'  class='button'>下一页</a>";
		} else {
			$this->pagingStr.="<a href=# class='button'>下一页</a>";
		}

		/* 尾页 */
		$this->pagingStr .= "<a id=paging_final href='{$this->uri}&{$this->pageSplitVar}={$this->lastPage}&{$this->pageNoVar}={$this->pageNo}' class='button'>尾页</a>";


		/* 每页显示条数 */
		$this->pagingStr .= "每页显示 <select name='{$this->pageNoVar}' size='1'
		     onchange='window.location=\"{$this->uri}&{$this->pageSplitVar}={$this->page}&{$this->pageNoVar}=\"+this.value'>\n";

		if (!isset($_GET[$this->pageNoVar])) {
			$this->pagingStr .= "<option value='{$this->pageNo}' selected>{$this->pageNo}</option>\n";
		}

		if ($this->totals < 10) {
			$this->pagingStr .= "<option value='{$this->pageNo}' selected>{$this->pageNo}</option>\n";
		}
		for($i=10; $i<=100; $i+=10){
			if($i == $this->pageNo) {
				$this->pagingStr .= "<option value='$i' selected>$i</option>\n";
			} else {
				$this->pagingStr .= "<option value='$i'>$i</option>\n";
			}
		}
		$this->pagingStr .= "</select> 条 ";

		/* 下拉跳转列表，循环列出所有页码 */
		$this->pagingStr .= " 跳到第 <select name='topage' size='1'
		     onchange='window.location=\"{$this->uri}&{$this->pageSplitVar}=\"+this.value+\"&{$this->pageNoVar}={$this->pageNo}\"'>\n";

		
		for($i=1; $i<=$this->lastPage; $i++){
			if($i > 1000000) {
				$i += 49999;
			} elseif($i > 100000) {
				$i += 4999;
			} elseif($i > 10000) {
				$i += 499;
			} elseif($i > 1000) {
				$i += 99;
			} elseif($i > 100) {
				$i += 49;
			} elseif($i > 50) {
				$i += 9;
			} 

			($i == $this->page) ?
			$this->pagingStr .= "<option value='$i' selected=\"selected\">$i</option>\n"
			:
			$this->pagingStr .= "<option value='$i'>$i</option>\n";
		}
		$this->pagingStr .= "</select> 页</div>";

		return true;
	}
}

class GoodsPaging extends UE_Paging {
	public function setStyle(){
		$this->pagingStr .= "
		<table width='100%' border='0' cellspacing='0' cellpadding='0' class='page_nav'>
		 <tr>
		 <td height='' align='left' valign='middle'>
		 <a id=paging_total class='button no-link' title='总主题数'>共{$this->totals}个</a></a>
		 <a id=paging_first href='{$this->uri}&{$this->pageNoVar}={$this->pageNo}'  class='button'>头页</a>";

		/* 前页 */
		if($this->prePage) {
			$this->pagingStr.="<a id=paging_last href='{$this->uri}&{$this->pageSplitVar}={$this->prePage}&{$this->pageNoVar}={$this->pageNo}' class='button'>上一页</a>";
		} else {
			$this->pagingStr.="<a class='button no-link' type='button'>上一页</a>";
		}

		/* 后页 */
		if($this->nextPage) {
			$this->pagingStr .= "<a id=paging_next href='{$this->uri}&{$this->pageSplitVar}={$this->nextPage}&{$this->pageNoVar}={$this->pageNo}'  class='button'>下一页</a>";
		} else {
			$this->pagingStr.="<a href=# class='button no-link'>下一页</a>";
		}

		/* 尾页 */
		$this->pagingStr .= "<a id=paging_final href='{$this->uri}&{$this->pageSplitVar}={$this->lastPage}&{$this->pageNoVar}={$this->pageNo}' class='button'>尾页</a>";

		if($this->lastPage) {
			/* 下拉跳转列表，循环列出所有页码 */
			$this->pagingStr .= " <select name='topage' class=\"topage\" size='1'>\n";
			for($i=1; $i<=$this->lastPage; $i++){
				if($i > 1000000) {
					$i += 49999;
				} elseif($i > 100000) {
					$i += 4999;
				} elseif($i > 10000) {
					$i += 499;
				} elseif($i > 1000) {
					$i += 99;
				} elseif($i > 100) {
					$i += 49;
				} elseif($i > 50) {
					$i += 9;
				} 
	
				($i == $this->page) ?
				$this->pagingStr .= "<option value='$i' selected=\"selected\">$i/{$this->lastPage}</option>\n"
				:
				$this->pagingStr .= "<option value='$i'>$i/{$this->lastPage}</option>\n";
			}
		    $this->pagingStr .= "</select> ";
		}
		
		$this->pagingStr .= " </td></tr></table>";

		return true;
	}

}
